﻿var Article = {
    version: '1.0.0.0.0',
    ArticleType: 2,
    GuideType: 3,
    Temp: 1,
    Published: 2,
    Focus: 4,
    PageIndex: 1,
    PageSize: 20,

    Init: function () {
        $('form').jqTransform();

        Article.DropDownListCategory("#ddlCategory", 0);
        Article.ListSearch(Article.PageIndex);

        $("#txtKeyword").keypress(function (event) {
            if (event.which == 13) {
                Article.ListSearch(Article.PageIndex);
                event.preventDefault();
            }
        });
    },

    ListSearch: function (pageIndex) {
        $('#toTop').click();

        var keyword = $('#txtKeyword').val();

        var categoryId = 0;
        $.each($('#ddlCategory ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlCategory select.jqTransformHidden option:eq(' + index + ')').attr('value');
                if (value > 0 && value != undefined)
                    categoryId = value;
            }
        });

        var status;
        $.each($('#ddlStatus ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlStatus select.jqTransformHidden option:eq(' + index + ')').attr('value');
                status = value;
            }
        });

        var orderBy;
        $.each($('#ddlOrderBy ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlOrderBy select.jqTransformHidden option:eq(' + index + ')').attr('value');
                orderBy = value;
            }
        });

        var pageSize = Article.PageSize;
        $.each($('#ddlPageSize ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlPageSize select.jqTransformHidden option:eq(' + index + ')').attr('value');
                pageSize = value;
            }
        });

        var url = String.format('/GetRequest.ashx?m=article&action=search&keyword={0}&cId={1}&status={2}&orderBy={3}&pi={4}&ps={5}&rand={6}',
                    keyword, categoryId, status, orderBy, pageIndex, pageSize, Math.random());

        $.getJSON(url, function (res) {
            if (res != null) {
                $('.pages').html(res.Pager);

                $.Mustache.load('/templates/article/list.htm')
                    .done(function () {
                        $('#listArticle tbody').html('');
                        $('#listArticle tbody').mustache('tpl-article-list', res);
                        $('.topDir').tipsy({ fade: true, gravity: 's' });
                        $('form').jqTransform();
                    });
            }
        });
    },

    DropDownListCategory: function (ddlArea, selected) {
        var html = '';
        
        var urlArticle = String.format('/GetRequest.ashx?m=category&action=listtree&tId={0}&rand={1}', Article.ArticleType, Math.random());
        $.getJSON(urlArticle, function (res) {
            if (res.Success) {
                if (res != null) {                    
                    html += '<select style="width: 160px" onchange="Article.ListSearch(Article.PageIndex);">';

                    if (res.Data.length > 0) {
                        html += '<option value="0">-- Tất cả --</option>';

                        $.each(res.Data, function (i, val) {
                            if (selected > 0 && val.CategoryId == selected)
                                html += String.format('<option value="{0}" selected="selected">{1}</option>', val.CategoryId, val.Name);
                            else
                                html += String.format('<option value="{0}">{1}</option>', val.CategoryId, val.Name);
                        });
                    }                    
                }
            }
        });

        var urlGuide = String.format('/GetRequest.ashx?m=category&action=listtree&tId={0}&rand={1}', Article.GuideType, Math.random());
        $.getJSON(urlGuide, function (res) {
            if (res.Success) {
                if (res != null) {
                    if (res.Data.length > 0) {
                        $.each(res.Data, function (i, val) {
                            if (selected > 0 && val.CategoryId == selected)
                                html += String.format('<option value="{0}" selected="selected">{1}</option>', val.CategoryId, val.Name);
                            else
                                html += String.format('<option value="{0}">{1}</option>', val.CategoryId, val.Name);
                        });
                    }

                    html += '</select>';

                    $(ddlArea).html(html);
                    $('form').jqTransform();
                }
            }
        });
    },

    Create: function () {
        window.location.href = "Default.aspx?cId=20";
    },

    ChangeStatus: function (username, status) {
        var count = 0;
        $.each($('#grdList tbody tr'), function () {
            var isSelected = $(this).find('td:eq(0) a');
            if ($(isSelected).hasClass('jqTransformChecked') == true) {
                count++;
                var articleId = ($(this).find('td:eq(0) input').attr('value'));                

                var url = String.format('/GetRequest.ashx?m=article&action=changestatus&username={0}&aId={1}&status={2}', username, articleId, status);
                $.post(url, function () { })
                    .done(function () {
                        Article.ListSearch(Article.PageIndex);
                    });
            }
        });

        if (count == 0)
            $.jGrowl(String.format('Bạn cần chọn bài viết để cập nhật <br/> trạng thái.'), { header: 'Cảnh báo:' });
        else
            $.jGrowl(String.format('Cập nhật trạng thái bài viết thành công!'), { header: 'Thông báo:' });
    },

    Delete: function (id) {
        $('#listArticle table tr').removeClass('gradeC');
        $('#a-' + id).addClass('gradeC');

        jConfirm('', 'Bạn có chắc chắn muốn xóa?', function (result) {
            if (result) {
                var url = String.format('/GetRequest.ashx?m=article&action=delete&aId={0}', id);
                $.post(url, function () { })
                    .done(function () {
                        $.jGrowl(String.format('Xóa bài viết thành công!'), { header: 'Thông báo:' });
                    });

                Article.ListSearch(Article.PageIndex);
            }
        });
    }
};